library(cjoint)
library(dplyr)
library(stringr)


extract_conjoint_outcome <- function(outcome_var) {
  # reads an outcome variable from the selected variable created by
  # read.qualtrics
  outcome <- read.qualtrics(filename = path_to_data, 
                            responses =  outcome_var,
                            new.format = TRUE)$selected
  
  return(outcome)
}

remove_incompletes <- function(survey_data){
  # filter out respondents who did not answer the survey
  survey_data[survey_data == 99] <- NA
  
  survey_data <- survey_data %>% 
    filter(!is.na(respondent))
  
  return(survey_data)
}

fix_variable_text <- function(survey_data){
  
  # turns the kanji original text into english versions
  
  survey_data <- survey_data %>% 
    mutate(local_status = if_else(
      str_detect(local_status, "todai_kyodai"), "Elite",
      if_else(str_detect(
        local_status, "respondent_prefecture"), "Local","Non-local"))
    )
  
  survey_data <- survey_data %>% 
    mutate(party = recode(
      formatted_data$party, 
      "自民党" = "LDP", 
      "立憲民主党" = "CDPJ",
      "日本共産党" = "CPJ",
      "国民民主党" = "DPP")
    )
  
  return(survey_data)
}

fix_variable_format <- function(survey_data){
  # turns variables into factors so they work with cregg
  
  survey_data$local_status <- as.factor(survey_data$local_status)
  survey_data$party <- as.factor(survey_data$party)
  
  return(survey_data) 
}


make_outcome_variables <- function(survey_data){
  
  # compiles individual policy items into broader categories
  
  number_of_programmatic_policies <- survey_data$gaiko + survey_data$nenkin + survey_data$seifusaimu
  number_of_pork <- survey_data$kigyo_yuchi + survey_data$kanko_sokushin + survey_data$kogyojigyo
  
  
  survey_data <- survey_data %>% 
    mutate(num_pork = number_of_pork,
           num_prog = number_of_programmatic_policies)
  
  return(survey_data)
}

make_new_vars <- function(survey_data){
  
  # Dichotomize some survey variables for easier subgroup comparisons 

  survey_data <- survey_data %>% 
    mutate(ldp_supporter = as.factor(if_else(Q42 == 1, 1, 0)),
           hi_kno = as.factor(if_else(Q50 > 3, 1, 0)),
           aichaku_both = as.factor(if_else((Q213_1 > 3) & (Q213_2 > 3), 1, 0)),
           aichaku = as.factor(if_else((Q213_1 > 3), 1, 0)),
           kakusa = as.factor(if_else((Q54 > 3), 1, 0)),
           rural = as.factor(if_else((Q211 < 2), 1, 0)))
  return(survey_data)
}

vote_outcome <- c("Q167", "Q172", "Q20")

kigyoyuuchi_outcome <- c("Q224_1", "Q225_1", "Q226_1")
kanko_sokushin_outcome <- c("Q224_2", "Q225_2", "Q226_2")
kogyojigyo_outcome <- c("Q224_3", "Q225_3", "Q226_3")
seifusaimu_outcome <- c("Q224_4", "Q225_4", "Q226_4")
gaiko_outcome <- c("Q224_5", "Q225_5", "Q226_5")
nenkin_outcome <- c("Q224_6", "Q225_6", "Q226_6")

local_region_problems <- c("Q227", "Q228", "Q229")

path_to_data <- 'data/follow_up_survey_rakuten.csv'
outcome_file <- "data/follow_up_survey_formatted.rds"

formatted_data <- read.qualtrics(filename = path_to_data, 
                                 responses =  vote_outcome,
                                 covar = c("ResponseId", "Duration..in.seconds.", "Q213_1", "Q213_2", "Q219_1", "Q42", "Q50", "Q54", 
                                           "Q22", "Q24", "Q28", "Q223", "Q32", "Q34", "Q211"),
                                 new.format = TRUE)

# these variables are not assigned the proper name by the read.qualtrics function.
# I fix this mistake here 
formatted_data$duration <- formatted_data$ResponseId
formatted_data$ResponseId <- formatted_data$Duration..in.seconds.



# reads in each policy item outcome 
formatted_data <- formatted_data %>% 
  mutate(kigyo_yuchi = extract_conjoint_outcome(kigyoyuuchi_outcome),
         kanko_sokushin = extract_conjoint_outcome(kanko_sokushin_outcome),
         kogyojigyo = extract_conjoint_outcome(kogyojigyo_outcome),
         seifusaimu = extract_conjoint_outcome(seifusaimu_outcome),
         gaiko = extract_conjoint_outcome(gaiko_outcome),
         nenkin = extract_conjoint_outcome(nenkin_outcome),
         local_affairs = extract_conjoint_outcome(local_region_problems))

# rename the vote outcome for clarity
formatted_data <- formatted_data %>% 
  rename(cand_vote = selected) 


formatted_data <- remove_incompletes(formatted_data)
formatted_data <- fix_variable_text(formatted_data)
formatted_data <- fix_variable_format(formatted_data)
formatted_data <- make_new_vars(formatted_data)
formatted_data <- make_outcome_variables(formatted_data)


saveRDS(formatted_data, file = outcome_file)

  
  



